package com.cyc.lock;

import java.util.concurrent.TimeUnit;

public class DeadLockDemo {
    public static void main(String[] args) {
        String lock1 = "A";
        String lock2 = "B";
        new Thread(new MytThread(lock1,lock2)).start();
        new Thread(new MytThread(lock2,lock1)).start();
    }
}

class MytThread implements Runnable{

    public MytThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    private String lockA;
    private String lockB;

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+" "+"得到锁"+lockA+",尝试获取锁"+lockB);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+" "+"得到锁"+lockB+"，尝试获取锁"+lockA);
            }
        }
    }
}
